<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/7/14
 * Time: 11:50
 */

namespace Home\Controller;

use Think\Controller;

class LoginController extends Controller
{
    /*登录*/
    public function login()
    {
        $this->display("login");
    }
    public function log()
    {
        //echo 123;die;
        if(IS_POST) {
            //echo 123;die;
            $tel = $_POST["tel"];

            $password=$_POST["password"];
            //var_dump($_POST);var_dump($password);die;
            if($tel==""||$password=="")
            {
                echo 1;
            }else{
                $res= M("register")->where("tel={$tel}")->find();
                if (!$res) {
                    //echo "手机号/用户名不存在";
                    echo 2;
                } else {
                    if (md5($password)!==$res['password']) {
                        echo 3;
                    }elseif($res['isok']==0){
                        echo 4;
                    }else {
                        $_SESSION["msg"]=$res;
                        echo 0;
                    }  
                }   
            }
        }else{
            $this->display("login");
        }
    }
    public function guodu(){
        $this->display('back');
    }
    //验证手机号，密码是否正确
    public function check()
    {
        //获得ajax提交的数据
        if($_POST["tel"]){
            $tel = $_POST["tel"];
            if($tel!=null) {
                $result = M("register")->where("tel={$tel}")->find();
                if (!$result) {
                    echo "1";
                }
            }
        }
        if ($_POST["password"]) {
            $password=md5($_POST["password"]);
            if($password!=null){
                //echo json_encode($password);die;
                $result = M("register")->where("password='{$password}'")->find();
                if (!$result) {
                    echo "2";
                }
            }
        }  
    }

    public function isok(){
        //获得ajax提交的数据
        if($_POST["tel"]){
            $tel = $_POST["tel"];
            if($tel!=null) {
                $result = M("register")->where("tel={$tel}")->find();
                if ($result['isok']==0) {
                    $data=array(
                        "info"=>"您还未通过审核"
                    );
                    $this->ajaxReturn($data);
                }
            }
        }
    }
    //找回密码
    public function findcode()
    {
        $this->display("code");
    }

    /*验证码*/
    public function verify()
    {

        $tel = $_POST["tel"];

        //生成随机验证码
        $code = "";
        $n = 6;
        for ($i = 1; $i <= $n; $i++) {
            $code .= rand(0, 9);
        }
        $time = time();
        $arr = array("mobile" => $tel, "verify" => $code, "time" => $time);
        $_SESSION["verify"] = $arr;

        code($tel, $code);

    }
    //验证手机号
    public function checkmobile()
    {
        //获得ajax提交的数据
        $tel = $_POST["tel"];
        if($tel!=null) {
            $result = M("register")->where("tel={$tel}")->find();
            if (!$result) {
                echo "1";
            }
        }
    }
    //重置密码
    public function resetcode()
    {
        if(IS_POST)
        {
            if (($_SESSION["verify"]["time"] + 60) < time()) {
                unset($_SESSION["verify"]);
                $this->error("验证码已过期，请重新获得！");
            }

            $tel = I("txt_UserName");
            $param['verify']=I("txt_UserName1");

           if ($param["verify"] == "")
            {
                $this->error("验证码不能为空！");
            }else if ($param["verify"] !== $_SESSION["verify"]["verify"]) {
                $this->error("验证码不一致！");
            }
            $param['password']=I("txtpwd");
            $param['passwordTwo']=I("txt_PwdTwo");

            $data = M("register");

            $re = $data->where("tel={$tel}")->data($param)->save();
            if ($re) {
                $this->redirect("login/login");
            } else {
                $this->redirect("login/findcode");
            }
        } else {
            $this->display("code");
        }

    }
}